package com.xthh._function;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;


import com.xt.pro.BLEControlJni;
import com.xthh.XTCore;
import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._enum.HH_EnumFileType;
import com.xthh._enum.HH_EnumTextAlgin;
import com.xthh._struct.HH_T_CustomLayerInfo;
import com.xthh._struct.HH_T_DeviceModel;
import com.xthh._struct.HH_T_LayerInfo;
import com.xthh._struct.HH_T_RGB;
import com.xthh._struct.HH_T_RGB_TJ;
import com.xthh._struct.HH_T_SC;
import com.xthh._struct.HH_T_SystemParams;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class HH_PicUtils {

    protected static final String TAG = "XTHH_PicUtils";

    protected static final int MAX_ARRAY_COLOR_FIX = 1024 * 1024 * 32;


    static final HH_T_RGB rgbBlack_I1 = new HH_T_RGB(53, 48, 44);
    static final HH_T_RGB rgbWhite_I1 = new HH_T_RGB(186, 182, 171);
    static final HH_T_RGB rgbRed_I1 = new HH_T_RGB(160, 60, 60);
    static final HH_T_RGB rgbYellow_I1 = new HH_T_RGB(205, 128, 40);
    static final HH_T_RGB rgbGreen_I1 = new HH_T_RGB(0, 255, 0);
    static final HH_T_RGB rgbBlue_I1 = new HH_T_RGB(0, 0, 255);
    static final HH_T_RGB rgbOrange_I1 = new HH_T_RGB(255, 128, 0);

    // 正常色
    static final HH_T_RGB rgbBlack = new HH_T_RGB(0,0,0);
    static final HH_T_RGB rgbWhite = new HH_T_RGB(255,255,255);
    static final HH_T_RGB rgbRed = new HH_T_RGB(255,0,0);
    static final HH_T_RGB rgbGreen = new HH_T_RGB(0,255,0);
    static final HH_T_RGB rgbBlue = new HH_T_RGB(0, 0,255);
    static final HH_T_RGB rgbYellow = new HH_T_RGB(255, 255, 0);
    static final HH_T_RGB rgbOrange = new HH_T_RGB(255, 128, 0);


    /**
     * 根据给定的宽和高进行拉伸
     *
     * @param origin    原图
     * @param newWidth  新图的宽
     * @param newHeight 新图的高
     * @return new Bitmap
     */
    public static Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (!origin.isRecycled()) {
            origin.recycle();
        }
        return newBM;
    }

    /**
     * 按比例缩放图片
     *
     * @param origin 原图
     * @param ratio  比例
     * @return 新的bitmap
     */
    public static Bitmap scaleBitmap(Bitmap origin, float ratio) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(ratio, ratio);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

    /**
     * 裁剪
     *
     * @param bitmap 原图
     * @return 裁剪后的图像
     */
    public static Bitmap cropBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int w = bitmap.getWidth(); // 得到图片的宽，高
        int h = bitmap.getHeight();
        return Bitmap.createBitmap(bitmap, 0, 0, w / 2, h, null, false);
    }

    /**
     * 选择变换
     *
     * @param origin 原图
     * @param alpha  旋转角度，可正可负
     * @return 旋转后的图片
     */
    public static Bitmap rotateBitmap(Bitmap origin, float alpha) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.setRotate(alpha);
        // 围绕原地进行旋转
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

    /**
     * 偏移效果
     *
     * @param origin 原图
     * @return 偏移后的bitmap
     */
    public static Bitmap skewBitmap(Bitmap origin) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.postSkew(-0.6f, -0.3f);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

    public static Bitmap rotationBitmap(Bitmap bitmap, int rotateDegree) {
        if (bitmap == null) {
            return null;
        }
        Matrix matrix = new Matrix(); // 创建操作图片用的矩阵对象
        matrix.postRotate(rotateDegree); // 执行图片的旋转动作
        // 创建并返回旋转后的位图对象
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, false);
    }

    /**
     * 垂直镜像
     *
     * @param a
     * @return
     */
    public static Bitmap convertVerMirro(Bitmap a) {
        if (a == null) {
            return null;
        }
        int w = a.getWidth();
        int h = a.getHeight();
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        Matrix m = new Matrix();
        m.postScale(1, -1);   //镜像垂直翻转
        // m.postScale(-1, 1);   //镜像水平翻转
        // m.postRotate(-90);  //旋转-90度
        Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
        cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()), new Rect(0, 0, w, h), null);
        return newb;
    }

    /**
     * 水平镜像
     *
     * @param a
     * @return
     */
    public static Bitmap convertHorMirro(Bitmap a) {
        if (a == null) {
            return null;
        }
        int w = a.getWidth();
        int h = a.getHeight();
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        Matrix m = new Matrix();
        //m.postScale(1, -1);   //镜像垂直翻转
        m.postScale(-1, 1);   //镜像水平翻转
        // m.postRotate(-90);  //旋转-90度
        Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
        cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()), new Rect(0, 0, w, h), null);
        return newb;
    }

    /**
     * 计算字体的宽度
     *
     * @param paint
     * @param str
     * @return
     */
    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }


    // c++ 里的算法，获取图片等比例缩放的位置及宽度
    public static Rect GetZoomPicSite(int nSrcPicWidth, int nSrcPicHeight, int width, int height) {
        // 获取目标图片宽、高、X、Y
        int nDstPicX = 0;
        int nDstPicY = 0;
        int nDstPicWidth = 0;
        int nDstPicHeight = 0;
        if (nSrcPicWidth <= width && nSrcPicHeight <= height) {
            // 源图片完全小于目标图片的，需要放大
            double fDst = height * 1.0 / width * 1.0;
            double fSrc = nSrcPicHeight * 1.0 / nSrcPicWidth * 1.0;
            if (fSrc > fDst) {
                // 这表明需要左右留空白
                nDstPicWidth = height * nSrcPicWidth / nSrcPicHeight;
                nDstPicHeight = height;
            } else {
                // 这表明需要上下留空白
                nDstPicWidth = width;
                nDstPicHeight = width * nSrcPicHeight / nSrcPicWidth;
            }
        } else if (nSrcPicWidth >= width && nSrcPicHeight >= height) {
            // 源图片完全大于目标图片的，需要缩小
            // 源图片完全小于目标图片的，需要放大
            double fDst = height * 1.0 / width * 1.0;
            double fSrc = nSrcPicHeight * 1.0 / nSrcPicWidth * 1.0;
            if (fSrc > fDst) {
                // 这表明需要左右留空白
                nDstPicWidth = height * nSrcPicWidth / nSrcPicHeight;
                nDstPicHeight = height;
            } else {
                // 这表明需要上下留空白
                nDstPicWidth = width;
                nDstPicHeight = width * nSrcPicHeight / nSrcPicWidth;
            }
        } else if (nSrcPicHeight >= height && nSrcPicWidth <= width) {
            // 源高大于新高，但源宽小于新宽
            nDstPicWidth = height * nSrcPicWidth / nSrcPicHeight;
            nDstPicHeight = height;
        } else if (nSrcPicHeight <= height && nSrcPicWidth >= width) {
            // 源高小于新高，但源宽大于新宽
            nDstPicWidth = width;
            nDstPicHeight = width * nSrcPicHeight / nSrcPicWidth;
        } else {
            nDstPicWidth = width;
            nDstPicHeight = height;
        }

        nDstPicX = (width - nDstPicWidth) / 2;
        nDstPicY = (height - nDstPicHeight) / 2;

        return new Rect(nDstPicX, nDstPicY, nDstPicX + nDstPicWidth, nDstPicY + nDstPicHeight);
    }


    // 等比例缩放图片
    public static int[] GetZoomImgBuffer(Bitmap bm) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), 100, 100);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));

        int cols = bmZoom.getWidth();
        int rows = bmZoom.getHeight();

        int pBitmapBuffer_A[] = new int[bmZoom.getWidth() * bmZoom.getHeight()];
        bmZoom.getPixels(pBitmapBuffer_A, 0, bmZoom.getWidth(), 0, 0, bmZoom.getWidth(), bmZoom.getHeight());

        return pBitmapBuffer_A;
    }
    public static Bitmap GetZoomImgBuffer2(Bitmap bm) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), 100, 100);
        return scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
    }

    // 等比例缩放图片
    public static Bitmap zoomImg2(Bitmap bm, int width, int height, int fillColor) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
        if (fillColor == 255)
            canvas.drawColor(Color.WHITE);
        else
            canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);
        return bmpRet;
    }

    // 等比例缩放图片
    public static Bitmap zoomImg2(Bitmap bm, int width, int height, int fillColor_r, int fillColor_g, int fillColor_b) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
         canvas.drawRGB(fillColor_r, fillColor_g, fillColor_b);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);
        return bmpRet;
    }


    /**
     * 使用Canvas
     *
     * @param bitmap 原始的Bitmap
     * @param rect   Bitmap被缩放放置的Rect
     * @return 缩放后的Bitmap
     */
    public static Bitmap scaleCanvas(Bitmap bitmap, Rect rect) {
        if (bitmap == null) {
            return null;
        }
        Bitmap newBitmap = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);//创建和目标相同大小的空Bitmap
        Canvas canvas = new Canvas(newBitmap);
        //canvas.drawColor(Color.GREEN);
        Paint paint = new Paint();
        Bitmap temp = bitmap;

        //针对绘制bitmap添加抗锯齿
        PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        paint.setFilterBitmap(true); //对Bitmap进行滤波处理
        paint.setAntiAlias(true);//设置抗锯齿
        canvas.setDrawFilter(pfd);
        canvas.drawBitmap(temp, null, rect, paint);

        return newBitmap;
    }

    public static Bitmap MakeBmpFromList(Bitmap bufferedImage, List<HH_T_RGB> list) {
        if (bufferedImage == null) {
            return null;
        }
        int nIndex = 0;
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();

        Bitmap bmpResult = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                HH_T_RGB rgb = list.get(nIndex);
                bmpResult.setPixel(i, j, Color.rgb(rgb.getR(), rgb.getG(), rgb.getB()));
                nIndex++;
            }
        }

        return bmpResult;
    }

    public static Bitmap DrawMacInfoToBitmap(Bitmap bitmapBWPic, String mac) {
        // 加片上加MAC地址
        Canvas canvas = new Canvas(bitmapBWPic);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // new antialised Paint
        paint.setColor(Color.rgb(255, 255, 255));
        paint.setTextSize((int) (24));// text size in pixels

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mac, 0, mac.length(), bounds);
        canvas.drawText(mac, bitmapBWPic.getWidth() - bounds.width(), bitmapBWPic.getHeight() - bounds.height(), paint);

        paint.setColor(Color.rgb(255, 0, 0));
        canvas.drawText(mac, bitmapBWPic.getWidth() - bounds.width() - 2, bitmapBWPic.getHeight() - bounds.height() - 2, paint);

        return bitmapBWPic;
    }

    public static Bitmap BitmapAddBkgPic(Bitmap src, int r, int g, int b) {
        if (src == null)
            return null;

        Bitmap copybitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());

        // 加片上加MAC地址
        Canvas canvas = new Canvas(copybitmap);
        canvas.drawRGB(r, g, b);
        Paint paint = new Paint();
        canvas.drawBitmap(src, new Matrix(), paint); // 复制原图

        return copybitmap;
    }



    /**
     * 通过资源id转化成Bitmap
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitmapById(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is);
    }

    // 制作纯色BMP
    public static Bitmap MadePureColorPic(HH_T_RGB rgb, int w, int h) {
        Bitmap bmpResult = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmpResult);
        canvas.drawARGB(255, rgb.getR(), rgb.getG(), rgb.getB());
        return bmpResult;
    }

    public static int GetTextWidth(HH_T_LayerInfo info) {
        if (info == null)
            return 0;
        Paint paint = new Paint();

        paint.setAntiAlias(false);// 关掉抗锯齿
        paint.setStyle(Paint.Style.FILL);// 画笔实心
        paint.setTextAlign(Paint.Align.LEFT); // 文本水平居左对齐

        if (HH_CharUtil.Strcmp(info.getFontName(), "DEFAULT_BOLD", true))
            paint.setTypeface(Typeface.DEFAULT_BOLD);
        else if (HH_CharUtil.Strcmp(info.getFontName(), "MONOSPACE", true))
            paint.setTypeface(Typeface.MONOSPACE);
        else if (HH_CharUtil.Strcmp(info.getFontName(), "SANS_SERIF", true))
            paint.setTypeface(Typeface.SANS_SERIF);
        else if (HH_CharUtil.Strcmp(info.getFontName(), "SERIF", true))
            paint.setTypeface(Typeface.SERIF);
        else
            paint.setTypeface(Typeface.DEFAULT);

        // 字体大小设置
        paint.setTextSize(info.getFontSize());

        // 先得到它要占用的宽和高，再计算出要画的点
        Rect rectDrawTxt = new Rect();
        paint.getTextBounds(info.getText(), 0, info.getText().length(), rectDrawTxt);

        return rectDrawTxt.width();
    }

    public static Bitmap GetSendPicBitmap(HH_T_DeviceModel td, int selectLayer, Context ctx, int idClose, int idRotate, int idCopyWidget) {

        if (td == null || td.getBkgBitmap() == null || td.getListLayerInfo() == null)
            return null;

        Bitmap src = td.getBkgBitmap();
        if (src == null){
            src = HH_PicUtils.GetBitmapFormFile(td.getPicPath());
        }
        if (src == null)
            return null;

        List<HH_T_LayerInfo> list = td.getListLayerInfo();
        if (list == null)
            return null;

        Bitmap copybitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());

        // 新建画布,关联bitmap
        Canvas canvas = new Canvas(copybitmap);
        Paint paint = new Paint();

        canvas.drawBitmap(src, new Matrix(), paint); // 复制原图

        for (int i = 0; i < list.size(); i++) {

            // 旋转
            //canvas.save();
            //canvas.rotate(90, list.get(i).getX(),nTxtCenterPointY - (rectDrawTxt.top + rectDrawTxt.bottom) / 2);
            //canvas.rotate(90, list.get(i).getX(), list.get(i).getY());

            paint = new Paint();

            if (selectLayer != -2){
                if (i == selectLayer) {
                    // 画选中的图层
                    paint.setStyle(Paint.Style.STROKE); // 空心样式
                    paint.setColor(Color.parseColor("#49a1f5"));
                    //设置线条的宽度，注意是 float 类型，在 Android 中最细的线条不是 1，可以比 1 更小
                    //更细。
                    paint.setStrokeWidth(2);// 线宽
                    //当绘图样式为 STROKE 时，该方法用于指定线条连接处的拐角样式，能使绘制的图形
                    //更加平滑。
                    paint.setStrokeJoin(Paint.Join.ROUND);

                    canvas.drawRect(new Rect(list.get(i).getX(), list.get(i).getY(), list.get(i).getX() + list.get(i).getW(), list.get(i).getY() + list.get(i).getH()), paint);
                } else {
                    // 这里也画虚线
                    paint.setStyle(Paint.Style.STROKE);//不加这个不显示
                    paint.setColor(Color.parseColor("#BEBEBE"));
                    paint.setStrokeWidth(2);
                    // 意思是所画虚线规则是先画5个长度的实线，留下5个长度的空白
                    PathEffect effects = new DashPathEffect(new float[]{5, 5}, 0);
                    paint.setPathEffect(effects);
                    Path mPath = new Path();
                    mPath.moveTo(list.get(i).getX() - 2, list.get(i).getY() - 2);
                    mPath.lineTo(list.get(i).getX() + list.get(i).getW() + 2, list.get(i).getY() - 2);

                    mPath.moveTo(list.get(i).getX() - 2, list.get(i).getY() + list.get(i).getH() + 2);
                    mPath.lineTo(list.get(i).getX() + list.get(i).getW() + 2, list.get(i).getY() + list.get(i).getH() + 2);
                    mPath.moveTo(list.get(i).getX() - 2, list.get(i).getY());
                    mPath.lineTo(list.get(i).getX() - 2, list.get(i).getY() + list.get(i).getH());
                    mPath.moveTo(list.get(i).getX() + list.get(i).getW() + 2, list.get(i).getY() + 2);
                    mPath.lineTo(list.get(i).getX() + list.get(i).getW() + 2, list.get(i).getY() + list.get(i).getH() + 2);
                    mPath.close();
                    canvas.drawPath(mPath, paint);
                }
            }



            // 根据图层类型来画
            switch (list.get(i).getLayerAttr()){
                case TYPE_TXT:{
                    // 文本图层
                    if (list.get(i).getBkg_r() >= 0 && list.get(i).getBkg_r() <= 255 &&
                            list.get(i).getBkg_g() >= 0 && list.get(i).getBkg_g() <= 255 &&
                            list.get(i).getBkg_b() >= 0 && list.get(i).getBkg_b() <= 255) {

                        paint.setStyle(Paint.Style.FILL); // 空心样式
                        paint.setColor(Color.rgb(list.get(i).getBkg_r(), list.get(i).getBkg_g(), list.get(i).getBkg_b()));
                        canvas.drawRect(new Rect(list.get(i).getX(), list.get(i).getY(), list.get(i).getX() + list.get(i).getW(), list.get(i).getY() + list.get(i).getH()), paint);
                    }
                    // 以下画文本
                    paint.setColor(Color.rgb(list.get(i).getFont_r(), list.get(i).getFont_g(), list.get(i).getFont_b()));
                    paint.setAntiAlias(false);// 关掉抗锯齿
                    paint.setStyle(Paint.Style.FILL);// 画笔实心
                    paint.setTextAlign(Paint.Align.LEFT); // 文本水平居左对齐

                    if (HH_CharUtil.Strcmp(list.get(i).getFontName(), "DEFAULT_BOLD", true))
                        paint.setTypeface(Typeface.DEFAULT_BOLD);
                    else if (HH_CharUtil.Strcmp(list.get(i).getFontName(), "MONOSPACE", true))
                        paint.setTypeface(Typeface.MONOSPACE);
                    else if (HH_CharUtil.Strcmp(list.get(i).getFontName(), "SANS_SERIF", true))
                        paint.setTypeface(Typeface.SANS_SERIF);
                    else if (HH_CharUtil.Strcmp(list.get(i).getFontName(), "SERIF", true))
                        paint.setTypeface(Typeface.SERIF);
                    else
                        paint.setTypeface(Typeface.DEFAULT);

                    // 字体大小设置
                    paint.setTextSize(list.get(i).getFontSize());
                    //paint.setTextSkewX(0.5f); // 设置文本倾斜度,取值0~1,正负表示倾斜方向
                    //paint.setUnderlineText(true);// 设置下划线
                    //paint.setFakeBoldText(true); // 设置粗体

                    paint.setFakeBoldText(list.get(i).isBold()); // 设置粗体

                    // 先得到它要占用的宽和高，再计算出要画的点
                    Rect rectDrawTxt = new Rect();
                    paint.getTextBounds(list.get(i).getText(), 0, list.get(i).getText().length(), rectDrawTxt);

                    if (rectDrawTxt.width() > list.get(i).getW()){
                        // 需要将字符串换行
                        /*TextPaint textPaint = new TextPaint();
                        textPaint.setColor(Color.rgb(list.get(i).getFont_r(), list.get(i).getFont_g(), list.get(i).getFont_b()));
                        textPaint.setTextSize(list.get(i).getFontSize());
                        textPaint.setFakeBoldText(list.get(i).isBold()); // 设置粗体
                        textPaint.setAntiAlias(true);*/

                        TextPaint textPaint = new TextPaint(paint);
                        StaticLayout layoutopen = new StaticLayout(list.get(i).getText(), textPaint, list.get(i).getW() , Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

                        canvas.save();
                        canvas.translate(list.get(i).getX(), list.get(i).getY());
                        layoutopen.draw(canvas);
                        canvas.restore();

                    } else {
                        int nTxtCenterPointX = list.get(i).getX() + list.get(i).getW() / 2;
                        int nTxtCenterPointY = list.get(i).getY() + list.get(i).getH() / 2;

                        if (list.get(i).getTextAlgin() == HH_EnumTextAlgin.CENTER) {
                            // 居中
                            canvas.drawText(list.get(i).getText(),
                                    nTxtCenterPointX - (rectDrawTxt.left + rectDrawTxt.right) / 2,
                                    nTxtCenterPointY - (rectDrawTxt.top + rectDrawTxt.bottom) / 2,
                                    paint);
                        } else if (list.get(i).getTextAlgin() == HH_EnumTextAlgin.LEFT) {
                            // 靠左
                            canvas.drawText(list.get(i).getText(),
                                list.get(i).getX(),
                                nTxtCenterPointY - (rectDrawTxt.top + rectDrawTxt.bottom) / 2,
                                paint);
                        } else {
                            // 靠右
                            canvas.drawText(list.get(i).getText(),
                                    nTxtCenterPointX - (rectDrawTxt.left + rectDrawTxt.right) + list.get(i).getW() / 2,
                                    nTxtCenterPointY - (rectDrawTxt.top + rectDrawTxt.bottom) / 2,
                                    paint);
                        }
                    }


                    break;
                }
                case TYPE_PIC:{
                    if (list.get(i).getBitmapPicLayer() == null){
                        list.get(i).setBitmapPicLayer(HH_PicUtils.GetBitmapFormFile(list.get(i).getPicPath()));
                    }
                    canvas.drawBitmap(list.get(i).getBitmapPicLayer(), list.get(i).getX(),list.get(i).getY(), paint);
                    break;
                }
                case TYPE_QR:{

                    Bitmap b = HH_SystemUtil.GetQrBitmap(list.get(i).getText(),
                            list.get(i).getW(),
                            list.get(i).getBkgRGB());
                    list.get(i).setBitmapPicLayer(b);

                    canvas.drawBitmap(list.get(i).getBitmapPicLayer(), list.get(i).getX(),list.get(i).getY(), paint);
                    break;
                }
                case TYPE_BAR: {
                    //Bitmap b = SystemUtil.GetBarBitmap(list.get(i).getText(), list.get(i).getW(), list.get(i).getH(), list.get(i).getBkgRGB());
                    //list.get(i).setBitmapPicLayer(b);
                    //canvas.drawBitmap(list.get(i).getBitmapPicLayer(), list.get(i).getX(),list.get(i).getY(), paint);
                    break;
                }
            }


            if (i == selectLayer && ctx != null) {
                // 画关闭和旋转按钮
                if (idClose > 0){
                    Bitmap bitmapClose = HH_PicUtils.readBitmapById(ctx, idClose);
                    canvas.drawBitmap(bitmapClose,
                            null,
                            list.get(i).getRectBtnClose(),
                            null);
                }

                /* //Bitmap bitmapRotate = HH_PicUtils.readBitmapById(ctx, idRotate);
                // 去掉旋转按钮
               canvas.drawBitmap(bitmapRotate,
                        null,
                        list.get(i).getRectBtnRotate(),
                        null);*/

                if (idCopyWidget > 0){
                    Bitmap bitmapCopyWidget = HH_PicUtils.readBitmapById(ctx, idCopyWidget);
                    canvas.drawBitmap(bitmapCopyWidget,
                            null,
                            list.get(i).getRectBtnCopyWidget(),
                            null);
                }


            }

            //canvas.restore();
        }


        return copybitmap;
    }

    public static void BitmapSaveFile(Bitmap bitmap, String savePath) {
        if (savePath == null) {
            return;
        }

        FileOutputStream os = null;
        try {
            os = new FileOutputStream(savePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件头
     */
    private static byte[] addBMPImageHeader(int size) {
        byte[] buffer = new byte[14];
        buffer[0] = 0x42;
        buffer[1] = 0x4D;
        buffer[2] = (byte) (size >> 0);
        buffer[3] = (byte) (size >> 8);
        buffer[4] = (byte) (size >> 16);
        buffer[5] = (byte) (size >> 24);
        buffer[6] = 0x00;
        buffer[7] = 0x00;
        buffer[8] = 0x00;
        buffer[9] = 0x00;
        buffer[10] = 0x36;
        buffer[11] = 0x00;
        buffer[12] = 0x00;
        buffer[13] = 0x00;
        return buffer;
    }

    /**
     * 文件信息头
     *
     * @param w
     * @param h
     * @return
     */
    private static byte[] addBMPImageInfosHeader(int w, int h) {
        byte[] buffer = new byte[40];
        buffer[0] = 0x28;
        buffer[1] = 0x00;
        buffer[2] = 0x00;
        buffer[3] = 0x00;
        buffer[4] = (byte) (w >> 0);
        buffer[5] = (byte) (w >> 8);
        buffer[6] = (byte) (w >> 16);
        buffer[7] = (byte) (w >> 24);
        buffer[8] = (byte) (h >> 0);
        buffer[9] = (byte) (h >> 8);
        buffer[10] = (byte) (h >> 16);
        buffer[11] = (byte) (h >> 24);
        buffer[12] = 0x01;
        buffer[13] = 0x00;
        buffer[14] = 0x18;
        buffer[15] = 0x00;
        buffer[16] = 0x00;
        buffer[17] = 0x00;
        buffer[18] = 0x00;
        buffer[19] = 0x00;
        buffer[20] = 0x00;
        buffer[21] = 0x00;
        buffer[22] = 0x00;
        buffer[23] = 0x00;
        buffer[24] = (byte) 0xE0;
        buffer[25] = 0x01;
        buffer[26] = 0x00;
        buffer[27] = 0x00;
        buffer[28] = 0x02;
        buffer[29] = 0x03;
        buffer[30] = 0x00;
        buffer[31] = 0x00;
        buffer[32] = 0x00;
        buffer[33] = 0x00;
        buffer[34] = 0x00;
        buffer[35] = 0x00;
        buffer[36] = 0x00;
        buffer[37] = 0x00;
        buffer[38] = 0x00;
        buffer[39] = 0x00;
        return buffer;
    }

    private static byte[] addBMP_RGB_888(int[] b, int w, int h) {
        int len = b.length;
        System.out.println(b.length);
        byte[] buffer = new byte[w * h * 3];
        int offset = 0;
        for (int i = len - 1; i >= w; i -= w) {
            //DIB文件格式最后一行为第一行，每行按从左到右顺序
            int end = i, start = i - w + 1;
            for (int j = start; j <= end; j++) {
                buffer[offset] = (byte) (b[j] >> 0);
                buffer[offset + 1] = (byte) (b[j] >> 8);
                buffer[offset + 2] = (byte) (b[j] >> 16);
                offset += 3;
            }
        }
        return buffer;
    }

    public static Bitmap GetBitmapFormFile(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static Bitmap PicAddIndex(Bitmap src, int i) {
        // 加片上加MAC地址
        Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // new antialised Paint
        paint.setColor(Color.rgb(0, 0, 0));
        paint.setTextSize((int) (96));// text size in pixels
        paint.setStrokeWidth (5);

        String drawInfo = String.format("%d", i);

        int x = bitmap.getWidth() / 2;
        int y = 100;
        Rect bounds = new Rect();
        paint.getTextBounds(drawInfo, 0, drawInfo.length(), bounds);
        canvas.drawText(drawInfo, x, y, paint);


        return bitmap;
    }

    // 从C移植过来的代码
    static List<Integer> GetRLEDataFormBin_0(List<Integer> src) {
        List<Integer> dst = new ArrayList<>();
        if (src == null)
            return dst;

        //DWORD dPutCount = 0;
        int dPutCount = 0;

        // char* pBuffer = new char[MAX_ARRAY_COLOR_FIX];
        // ZeroMemory(pBuffer, MAX_ARRAY_COLOR_FIX);
        // char* dst = new char[MAX_ARRAY_COLOR_FIX];
        // ZeroMemory(dst, MAX_ARRAY_COLOR_FIX);

        int dSize11 = src.size();


        if (dSize11 > 0) {
            int nOldData = 9999;
            HH_T_SC temSC = new HH_T_SC();
            int nNewSize = dSize11 / 2;
            for (int i = 0; i < nNewSize; i++) {
                if (nOldData != src.get(i)) {
                    temSC.setLen(0);
                    nOldData = src.get(i);
                }

                if (temSC.getLen() == 0) {
                    int mm = temSC.getLen() + 1;
                    temSC.setLen(mm);
                    //temSC.len++;
                    temSC.setB(src.get(i));
                    //temSC.b = (byte)pBuffer[i];
                } else {
                    int mm = temSC.getLen() + 1;
                    temSC.setLen(mm);
                    //temSC.len++;
                }

                int bb1 = src.get(i + 1);
                int bb2 = temSC.getB();

                if ((i + 1 < nNewSize && bb1 != bb2) || i == nNewSize - 1) {
                    int nC = (temSC.getLen() % 255 == 0) ? (temSC.getLen() / 255) : (temSC.getLen() / 255 + 1);
                    for (int m = 0; m < nC; m++) {
                        if (m == nC - 1) {
                            if (dPutCount + 2 < MAX_ARRAY_COLOR_FIX) {
                                dst.add((temSC.getLen() % 255 == 0) ? 255 : (temSC.getLen() % 255));
                                dst.add(temSC.getB());
                            }
                        } else {
                            if (dPutCount + 2 < MAX_ARRAY_COLOR_FIX) {
                                dst.add(255);
                                dst.add(temSC.getB());
                            }
                        }
                    }
                }
            }
        }

        HH_Logging.hexI(TAG, dst, 100);
        //string sResult(dst, dPutCount);
        // XTHHP_OutputDebugString("len =%d", dPutCount);

        //delete[]dst;
        // dst = NULL;

        // delete[]pBuffer;
        // pBuffer = NULL;

        return dst;
    }

    // 从C移植过来的代码
    static List<Integer> GetRLEDataFormBin_1(List<Integer> src) {
        List<Integer> dst = new ArrayList<>();
        if (src == null)
            return dst;

        //DWORD dPutCount = 0;
        int dPutCount = 0;

        // char* pBuffer = new char[MAX_ARRAY_COLOR_FIX];
        // ZeroMemory(pBuffer, MAX_ARRAY_COLOR_FIX);
        // char* dst = new char[MAX_ARRAY_COLOR_FIX];
        // ZeroMemory(dst, MAX_ARRAY_COLOR_FIX);

        int dSize11 = src.size();


        if (dSize11 > 0) {
            int nOldData = 9999;
            HH_T_SC temSC = new HH_T_SC();
            //int nNewSize = dSize11 / 2;

            for (int i = dSize11 / 2; i < dSize11; i++) {
                if (nOldData != src.get(i)) {
                    temSC.setLen(0);
                    nOldData = src.get(i);
                }

                if (temSC.getLen() == 0) {
                    int mm = temSC.getLen() + 1;
                    temSC.setLen(mm);
                    //temSC.len++;
                    temSC.setB(src.get(i));
                    //temSC.b = (byte)pBuffer[i];
                } else {
                    int mm = temSC.getLen() + 1;
                    temSC.setLen(mm);
                    //temSC.len++;
                }

                int bb1 = (i + 1 < src.size()) ? src.get(i + 1) : 0;
                int bb2 = temSC.getB();

                if ((i + 1 < dSize11 && bb1 != bb2) || i == dSize11 - 1) {
                    int nC = (temSC.getLen() % 255 == 0) ? (temSC.getLen() / 255) : (temSC.getLen() / 255 + 1);
                    for (int m = 0; m < nC; m++) {
                        if (m == nC - 1) {
                            if (dPutCount + 2 < MAX_ARRAY_COLOR_FIX) {
                                dst.add((temSC.getLen() % 255 == 0) ? 255 : (temSC.getLen() % 255));
                                dst.add(temSC.getB());
                                // dst[dPutCount++] = (temSC.getLen() % 255 == 0) ? 255 : (temSC.getLen() % 255);
                                //dst[dPutCount++] = temSC.getB();
                            }
                        } else {
                            if (dPutCount + 2 < MAX_ARRAY_COLOR_FIX) {
                                dst.add(255);
                                dst.add(temSC.getB());
                                //dst[dPutCount++] = 255;
                                //dst[dPutCount++] = temSC.getB();
                            }
                        }
                    }
                }
            }
        }

        //string sResult(dst, dPutCount);
        // XTHHP_OutputDebugString("len =%d", dPutCount);

        //delete[]dst;
        // dst = NULL;

        // delete[]pBuffer;
        // pBuffer = NULL;

        return dst;
    }



    // 等比例缩放图片
    public static Bitmap zoomImg4(Bitmap bm, int width, int height) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        return bmZoom;
    }


    // deviceColorType,0黑白，1黑白红，2黑白黄，3黑白红黄，5黑白红黄绿蓝橙色
    // useCoefficient 是否使用扩散
    public static Bitmap PicDither(Bitmap srcBitmp, HH_EnumDeviceColorType deviceColorType, boolean useCoefficient, int code1, int deviceNumber) {
        if (srcBitmp == null)
            return null;

        try {
            int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
            srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());


            int ditherType = 0;
            if (useCoefficient){
                ditherType = (code1 == 1) ? 1 : 2;
            }

            int[] pResultBuffer = BLEControlJni.PicDither7(pSrcBitmapBitmap,
                    srcBitmp.getWidth(), srcBitmp.getHeight(),
                    deviceColorType.ordinal(), ditherType);
            if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
                return null;

            int cols = srcBitmp.getWidth();
            int rows = srcBitmp.getHeight();
            Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
            int index = 0;
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    int rgb = pResultBuffer[index++];
                    int r = Color.red(rgb);
                    int g = Color.green(rgb);
                    int b = Color.blue(rgb);
                    bmpResult.setPixel(col, row, Color.rgb(r, g, b));
                }
            }

            return bmpResult;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Bitmap PicDither(Bitmap srcBitmp, HH_EnumDeviceColorType deviceColorType, int ditherType, int pointCount) {
        HH_Logging.i(TAG, "PicDither:%d", ditherType);
        if (srcBitmp == null)
            return null;

        switch (ditherType){
            case 0:{
                // 色阶
                return PD9(srcBitmp, deviceColorType.ordinal(), 0, 19,
                        pointCount);
            }
            case 1:{
                // 正常
                return PD9(srcBitmp, deviceColorType.ordinal(), 1, 19,
                        pointCount);
            }
            case 2:{
                // 调暗
                return PD9(srcBitmp, deviceColorType.ordinal(), 1, 21,
                        pointCount);
            }
            case 3:{
                // 调亮
                return PD9(srcBitmp, deviceColorType.ordinal(), 1, 20,
                        pointCount);
            }
        }

        return null;
    }

    // deviceColorType,0黑白，1黑白红，2黑白黄，3黑白红黄，5黑白红黄绿蓝橙色
    // useCoefficient 是否使用扩散
    // type 0色阶，其它的都是抖点
    public static Bitmap PicDither2(HH_T_SystemParams systemParams, Bitmap srcBitmp, HH_EnumDeviceColorType deviceColorType, int ditherType, int pointCount) {
        if (srcBitmp == null)
            return null;

        try {
            if (ditherType >= 1 && ditherType <= 48){
                // 里面的循环共48个
                // 这里面是手动调亮度和扩散系数的
                int ddIndex = 0;
                for (int bright = 5; bright <= 10; bright++){
                    for (int ditherPoint = 5; ditherPoint <= 12; ditherPoint++){
                        ddIndex++;
                        if (ditherType == ddIndex){

                            int cols = srcBitmp.getWidth();
                            int rows = srcBitmp.getHeight();
                            int bufferLen = cols * rows * 3;
                            int[] pReadBuffer = new int[bufferLen];
                            int index = 0;
                            for (int row = 0; row < rows; row++) {
                                for (int col = 0; col < cols; col++) {
                                    int pixel = srcBitmp.getPixel(col, row);
                                    pReadBuffer[index++] = Color.red(pixel);
                                    pReadBuffer[index++] = Color.green(pixel);
                                    pReadBuffer[index++] = Color.blue(pixel);
                                }
                            }
                            int[] pResultBuffer = new int[bufferLen];
                            pResultBuffer = BLEControlJni.PicDither(pReadBuffer, cols, rows, deviceColorType.ordinal(), 1, bright, ditherPoint, 0, 0);
                            if (pResultBuffer == null || pResultBuffer.length != bufferLen)
                                return null;

                            Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
                            index = 0;
                            for (int row = 0; row < rows; row++) {
                                for (int col = 0; col < cols; col++) {
                                    int r = pResultBuffer[index++];
                                    int g = pResultBuffer[index++];
                                    int b = pResultBuffer[index++];
                                    int nColor = Color.rgb(r, g, b);
                                    bmpResult.setPixel(col, row, nColor);
                                }
                            }

                            return bmpResult;
                        }
                    }
                }
            } else {
                switch (ditherType){
                    case 0:{

                        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
                        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

                        int[] pResultBuffer = BLEControlJni.PicDither7(pSrcBitmapBitmap,
                                srcBitmp.getWidth(), srcBitmp.getHeight(),
                                deviceColorType.ordinal(), 0);
                        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
                            return null;

                        int cols = srcBitmp.getWidth();
                        int rows = srcBitmp.getHeight();
                        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
                        int index = 0;
                        for (int row = 0; row < rows; row++) {
                            for (int col = 0; col < cols; col++) {
                                int rgb = pResultBuffer[index++];
                                int r = Color.red(rgb);
                                int g = Color.green(rgb);
                                int b = Color.blue(rgb);
                                bmpResult.setPixel(col, row, Color.rgb(r, g, b));
                            }
                        }

                        return bmpResult;
                        //break;
                    }
                    case 49:{
                        return PD3(srcBitmp, deviceColorType.ordinal(), 1);
                    }
                    case 50:{
                        return PD3(srcBitmp, deviceColorType.ordinal(), 2);
                    }
                    case 51:{
                        return PD4(srcBitmp, deviceColorType.ordinal(), 1);
                    }
                    case 52:{
                        return PD4(srcBitmp, deviceColorType.ordinal(), 2);
                    }
                    case 53:{
                        // 0是抖点
                        return PD6(srcBitmp, deviceColorType.ordinal(), 0);
                    }
                    case 54:{
                        return PD7(srcBitmp, deviceColorType.ordinal(), 1);
                    }
                    case 55:{
                        return PD7(srcBitmp, deviceColorType.ordinal(), 2);
                    }
                    case 56:{
                        return PD2(srcBitmp, deviceColorType.ordinal(), 1);
                    }
                    case 57:{
                        return PD2(srcBitmp, deviceColorType.ordinal(), 2);
                    }
                    case 58:{
                        return PD8(srcBitmp, deviceColorType.ordinal(), 1);
                    }
                    case 59:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 0,
                                pointCount);
                    }
                    case 60:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 1,
                                pointCount);
                    }
                    case 61:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 2,
                                pointCount);
                    }
                    case 62:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 3,
                                pointCount);
                    }
                    case 63:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 4,
                                pointCount);
                    }
                    case 64:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 5,
                                pointCount);
                    }
                    case 65:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 6,
                                pointCount);
                    }
                    case 66:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 7,
                                pointCount);
                    }
                    case 67:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 8,
                                pointCount);
                    }
                    case 68:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 9,
                                pointCount);
                    }
                    case 69:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 10,
                                pointCount);
                    }
                    case 70:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 11,
                                pointCount);
                    }
                    case 71:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 12,
                                pointCount);
                    }
                    case 72:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 13,
                                pointCount);
                    }
                    case 73:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 14,
                                pointCount);
                    }
                    case 74:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 15,
                                pointCount);
                    }
                    case 75:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 16,
                                pointCount);
                    }
                    case 76:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 77:{
                        return PD9(PicAddRed(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 78:{
                        return PD9(PicAddRed(srcBitmp, 20), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 79:{
                        return PD9(PicAddRed(srcBitmp, 30), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 80:{
                        return PD9(PicAddRed(srcBitmp, 40), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 81:{
                        return PD9(PicAddRed(srcBitmp, 50), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 82:{
                        return PD9(PicAddRed(srcBitmp, 60), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 83:{
                        return PD9(PicAddRed(srcBitmp, 70), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 84:{
                        return PD9(PicAddRed(srcBitmp, 80), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 85:{
                        return PD9(PicAddRed(srcBitmp, 90), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 86:{
                        return PD9(PicAddRed(srcBitmp, 100), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }

                    case 87:{
                        return PD9(PicAddGreen(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 88:{
                        return PD9(PicAddGreen(srcBitmp, 20), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 89:{
                        return PD9(PicAddGreen(srcBitmp, 30), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 90:{
                        return PD9(PicAddGreen(srcBitmp, 40), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 91:{
                        return PD9(PicAddGreen(srcBitmp, 50), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 92:{
                        return PD9(PicAddGreen(srcBitmp, 60), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 93:{
                        return PD9(PicAddGreen(srcBitmp, 70), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 94:{
                        return PD9(PicAddGreen(srcBitmp, 80), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 95:{
                        return PD9(PicAddGreen(srcBitmp, 90), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 96:{
                        return PD9(PicAddGreen(srcBitmp, 100), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }


                    case 97:{
                        return PD9(PicAddBlue(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 98:{
                        return PD9(PicAddBlue(srcBitmp, 20), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 99:{
                        return PD9(PicAddBlue(srcBitmp, 30), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 100:{
                        return PD9(PicAddBlue(srcBitmp, 40), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 101:{
                        return PD9(PicAddBlue(srcBitmp, 50), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 102:{
                        return PD9(PicAddBlue(srcBitmp, 60), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 103:{
                        return PD9(PicAddBlue(srcBitmp, 70), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 104:{
                        return PD9(PicAddBlue(srcBitmp, 80), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 105:{
                        return PD9(PicAddBlue(srcBitmp, 90), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 106:{
                        return PD9(PicAddBlue(srcBitmp, 100), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 107:{
                        return PD9(PicAddRGB(srcBitmp, 10, 10, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 108:{
                        return PD9(PicAddRed(srcBitmp, 100), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 109:{

                        return PD9(PicAddRed(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 110:{
                        // 压缩，0-100,100表示不压
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        srcBitmp.compress(Bitmap.CompressFormat.JPEG, 30, out);
                        return PD9(PicAddRed(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 111:{

                        final CountDownLatch latch = new CountDownLatch(1);
                        final Bitmap[] result = {null};
                        // 循环压缩
                        HH_Logging.i(TAG, "com begin");
                        String tempSavePath = HH_SystemUtil.GetTempPath(systemParams,10, HH_EnumFileType.JPG);
                        CompressImageByQuality(srcBitmp, tempSavePath, 10, new ResultCallBackWrite() {
                            @Override
                            public void callback(String writeResult) {
                                HH_Logging.i(TAG, "com callback path = %s", writeResult);

                                // 压缩，0-100,100表示不压
                                result[0] = PD9(PicAddRed(srcBitmp, 10), deviceColorType.ordinal(), 1, 17,
                                        pointCount);
                                latch.countDown();
                            }
                        });
                        try {
                            latch.await();
                        } catch (InterruptedException e) {
                        }
                        HH_Logging.i(TAG, "com end");

                        return result[0];

                    }
                    case 112:{
                        int srcW = srcBitmp.getWidth();
                        int srcH = srcBitmp.getHeight();
                        Bitmap b1 = HH_PicUtils.zoomImg2(srcBitmp, srcBitmp.getWidth() / 3, srcBitmp.getHeight() / 3);

                        String tempSavePath = HH_SystemUtil.GetTempPath(systemParams,1, HH_EnumFileType.JPG);
                        HH_PicUtils.BitmapSaveFile(b1, tempSavePath);

                        Bitmap b2 = HH_PicUtils.GetBitmapFormFile(tempSavePath);

                        b2 = HH_PicUtils.zoomImg2(b2, srcW, srcH);
                        return PD9(PicAddRed(srcBitmp, 60), deviceColorType.ordinal(), 1, 17,
                                pointCount);
                    }
                    case 113:{
                        return PD9(srcBitmp, deviceColorType.ordinal(), 1, 18,
                                pointCount);
                    }
                    default:
                        return null;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Bitmap convertBitmapTo16BitBMP(Bitmap srcBitmp) {

        Bitmap.Config config = Bitmap.Config.ARGB_4444; // 16位颜色深度配置
        return srcBitmp.copy(config, false);
    }

    public interface ResultCallBackWrite {
        public void callback(String writeResult);
    }


    // 压缩图片 范围0-100
    public static int options = 0;
    // compressParams 1-20
    public static void CompressImageByQuality(final Bitmap bitmap, final String imgPath, int compressParams, final ResultCallBackWrite resultCallBack){
        if (compressParams < 1)
            compressParams = 1;
        if (compressParams > 20)
            compressParams = 20;

        HH_Logging.i(TAG, "CompressImageByQuality begin-%d", options);
        int finalCompressParams = compressParams;
        new Thread(new Runnable() {//开启多线程进行压缩处理
            @Override
            public void run() {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                options = 20;
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法，把压缩后的数据存放到baos中 (100表示不压缩，0表示压缩到最小)
                while (baos.toByteArray().length / 1024 > finalCompressParams) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩
                    HH_Logging.i(TAG, "CompressImageByQuality here-%d", options);
                    baos.reset();//重置baos即让下一次的写入覆盖之前的内容
                    options -= 10;//图片质量每次减少10
                    if(options<0)options=0;//如果图片质量小于10，则将图片的质量压缩到最小值
                    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中
                    if(options==0)
                        break;//如果图片的质量已降到最低则，不再进行压缩
                }
                try {
                    FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中
                    fos.write(baos.toByteArray());
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                resultCallBack.callback(imgPath);
            }
        }).start();

        HH_Logging.i(TAG, "CompressImageByQuality end-%d", options);
    }

    // value = 0-255
    public static Bitmap PicAddRGB(Bitmap bt, int r, int g, int b){
        float[] src = new float[]{
                1, 0, 0, 0, r,
                0, 1, 0, 0, g,
                0, 0, 1, 0, b,
                0, 0, 0, 1, 0};

        // 3.定义ColorMatrix，并指定RGBA矩阵
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(src);


        Bitmap bmp = Bitmap.createBitmap(bt.getWidth(), bt.getHeight(),
                Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bt, 0, 0, paint);
        return bmp;
    }


    // value = 0-255
    public static Bitmap PicAddRed(Bitmap bt, int value){
        float[] src = new float[]{
                1, 0, 0, 0, value,
                0, 1, 0, 0, 0,
                0, 0, 1, 0, 0,
                0, 0, 0, 1, 0};

        // 3.定义ColorMatrix，并指定RGBA矩阵
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(src);


        Bitmap bmp = Bitmap.createBitmap(bt.getWidth(), bt.getHeight(),
                Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bt, 0, 0, paint);
        return bmp;
    }

    // value = 0-255
    public static Bitmap PicAddGreen(Bitmap bt, int value){
        float[] src = new float[]{
                1, 0, 0, 0, 0,
                0, 1, 0, 0, value,
                0, 0, 1, 0, 0,
                0, 0, 0, 1, 0};

        // 3.定义ColorMatrix，并指定RGBA矩阵
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(src);


        Bitmap bmp = Bitmap.createBitmap(bt.getWidth(), bt.getHeight(),
                Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bt, 0, 0, paint);
        return bmp;
    }


    // value = 0-255
    public static Bitmap PicAddBlue(Bitmap bt, int value){
        float[] src = new float[]{
                1, 0, 0, 0, 0,
                0, 1, 0, 0, 0,
                0, 0, 1, 0, value,
                0, 0, 0, 1, 0};

        // 3.定义ColorMatrix，并指定RGBA矩阵
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(src);


        Bitmap bmp = Bitmap.createBitmap(bt.getWidth(), bt.getHeight(),
                Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bt, 0, 0, paint);
        return bmp;
    }


    public static Bitmap PD4(Bitmap srcBitmp, int deviceColorType, int ditherType) {
        if (srcBitmp == null)
            return null;

        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

        int[] pResultBuffer = BLEControlJni.PicDither4(pSrcBitmapBitmap,
                srcBitmp.getWidth(), srcBitmp.getHeight(),
                deviceColorType, ditherType);
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                bmpResult.setPixel(col, row, pResultBuffer[index++]);
            }
        }

        return bmpResult;
    }


    public static Bitmap PD8(Bitmap srcBitmp, int deviceColorType, int ditherType) {
        if (srcBitmp == null)
            return null;

        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

        int[] pResultBuffer = BLEControlJni.PicDither8(pSrcBitmapBitmap,
                srcBitmp.getWidth(), srcBitmp.getHeight(),
                deviceColorType, ditherType);
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                bmpResult.setPixel(col, row, pResultBuffer[index++]);
            }
        }

        return bmpResult;
    }


    public static Bitmap PD9(Bitmap srcBitmp, int deviceColorType, int ditherType, int params, int pointCount) {
        if (srcBitmp == null)
            return null;


        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

        int[] pResultBuffer = null;
        if (ditherType == 1){
            switch (params){
                case 0:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            0,
                             pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 1:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 2:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                //
                case 3:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 4:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 5:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                //
                case 6:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 7:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 8:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            25,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }

                case 9:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 10:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 11:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                //
                case 12:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 13:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 14:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                //
                case 15:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            210,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 16:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            150,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            5,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 17:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            rgbRed.getR(),rgbRed.getG(),rgbRed.getB(),
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            15,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 18:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack.getR(),rgbBlack.getG(),rgbBlack.getB(),
                            rgbWhite.getR(),rgbWhite.getG(),rgbWhite.getB(),
                            100,0,0,
                            rgbGreen.getR(),rgbGreen.getG(),rgbGreen.getB(),
                            rgbBlue.getR(),rgbBlue.getG(),rgbBlue.getB(),
                            rgbYellow.getR(),rgbYellow.getG(),rgbYellow.getB(),
                            rgbOrange.getR(),rgbOrange.getG(),rgbOrange.getB(),
                            15,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 19:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack_I1.getR(),rgbBlack_I1.getG(),rgbBlack_I1.getB(),
                            rgbWhite_I1.getR(),rgbWhite_I1.getG(),rgbWhite_I1.getB(),
                            rgbRed_I1.getR(),rgbRed_I1.getG(),rgbRed_I1.getB(),
                            rgbGreen_I1.getR(),rgbGreen_I1.getG(),rgbGreen_I1.getB(),
                            rgbBlue_I1.getR(),rgbBlue_I1.getG(),rgbBlue_I1.getB(),
                            rgbYellow_I1.getR(),rgbYellow_I1.getG(),rgbYellow_I1.getB(),
                            rgbOrange_I1.getR(),rgbOrange_I1.getG(),rgbOrange_I1.getB(),
                            0,
                            0,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 20:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack_I1.getR(),rgbBlack_I1.getG(),rgbBlack_I1.getB(),
                            rgbWhite_I1.getR(),rgbWhite_I1.getG(),rgbWhite_I1.getB(),
                            rgbRed_I1.getR(),rgbRed_I1.getG(),rgbRed_I1.getB(),
                            rgbGreen_I1.getR(),rgbGreen_I1.getG(),rgbGreen_I1.getB(),
                            rgbBlue_I1.getR(),rgbBlue_I1.getG(),rgbBlue_I1.getB(),
                            rgbYellow_I1.getR(),rgbYellow_I1.getG(),rgbYellow_I1.getB(),
                            rgbOrange_I1.getR(),rgbOrange_I1.getG(),rgbOrange_I1.getB(),
                            0,
                            1,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
                case 21:{
                    pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                            srcBitmp.getWidth(), srcBitmp.getHeight(),
                            deviceColorType, ditherType,// 0 色阶，1抖点，
                            rgbBlack_I1.getR(),rgbBlack_I1.getG(),rgbBlack_I1.getB(),
                            rgbWhite_I1.getR(),rgbWhite_I1.getG(),rgbWhite_I1.getB(),
                            rgbRed_I1.getR(),rgbRed_I1.getG(),rgbRed_I1.getB(),
                            rgbGreen_I1.getR(),rgbGreen_I1.getG(),rgbGreen_I1.getB(),
                            rgbBlue_I1.getR(),rgbBlue_I1.getG(),rgbBlue_I1.getB(),
                            rgbYellow_I1.getR(),rgbYellow_I1.getG(),rgbYellow_I1.getB(),
                            rgbOrange_I1.getR(),rgbOrange_I1.getG(),rgbOrange_I1.getB(),
                            0,
                            2,
                            pointCount);  //1调亮，2调暗，0正常值
                    break;
                }
            }
        } else {
            pResultBuffer = BLEControlJni.PicDither9(pSrcBitmapBitmap,
                    srcBitmp.getWidth(), srcBitmp.getHeight(),
                    deviceColorType, 0,// 0 色阶，1抖点，
                    rgbBlack_I1.getR(),rgbBlack_I1.getG(),rgbBlack_I1.getB(),
                    rgbWhite_I1.getR(),rgbWhite_I1.getG(),rgbWhite_I1.getB(),
                    rgbRed_I1.getR(),rgbRed_I1.getG(),rgbRed_I1.getB(),
                    rgbGreen_I1.getR(),rgbGreen_I1.getG(),rgbGreen_I1.getB(),
                    rgbBlue_I1.getR(),rgbBlue_I1.getG(),rgbBlue_I1.getB(),
                    rgbYellow_I1.getR(),rgbYellow_I1.getG(),rgbYellow_I1.getB(),
                    rgbOrange_I1.getR(),rgbOrange_I1.getG(),rgbOrange_I1.getB(),
                    0,
                    2,
                    pointCount);  //1调亮，2调暗，0正常值
        }
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        //b2 = XTCore.ChangeBitmapColor(b2, deviceColorType);

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {

                // 要转下，要不然数据对不上
                int rolor = pResultBuffer[index++];
                int r = Color.red(rolor);
                int g = Color.green(rolor);
                int b = Color.blue(rolor);
                HH_T_RGB rgb = new HH_T_RGB(r,g,b);
                HH_T_RGB res = ColorConvert(rgb);

                bmpResult.setPixel(col, row, Color.rgb(res.getR(), res.getG(), res.getB()));
            }
        }

        return bmpResult;
    }




    public static Bitmap PD3(Bitmap srcBitmp, int deviceColorType, int ditherType) {
        if (srcBitmp == null)
            return null;

        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

        int[] pResultBuffer = BLEControlJni.PicDither3(pSrcBitmapBitmap,
                srcBitmp.getWidth(), srcBitmp.getHeight(),
                deviceColorType, ditherType);
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                bmpResult.setPixel(col, row, pResultBuffer[index++]);
            }
        }

        return bmpResult;
    }


    public static Bitmap PD6(Bitmap srcBitmp, int deviceColorType, int ditherType) {
        if (srcBitmp == null)
            return null;

        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());

        int[] pResultBuffer = BLEControlJni.PicDither6(pSrcBitmapBitmap,
                srcBitmp.getWidth(), srcBitmp.getHeight(),
                deviceColorType, ditherType);
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                bmpResult.setPixel(col, row, pResultBuffer[index++]);
            }
        }

        return bmpResult;
    }



    // deviceColorType,0黑白，1黑白红，2黑白黄，3黑白红黄，5黑白红黄绿蓝橙色
    // useCoefficient 是否使用扩散
    public static Bitmap PD2(Bitmap srcBitmp, int deviceColorType, int ditherType) {
        if (srcBitmp == null)
            return null;

        int []zoomBuffer =  GetZoomImgBuffer(srcBitmp);

        int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
        srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());


        int[] pResultBuffer = BLEControlJni.PicDither2(pSrcBitmapBitmap,
                zoomBuffer, zoomBuffer.length,
                srcBitmp.getWidth(), srcBitmp.getHeight(),
                deviceColorType, ditherType,
                0);
        if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
            return null;

        int cols = srcBitmp.getWidth();
        int rows = srcBitmp.getHeight();
        Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                bmpResult.setPixel(col, row, pResultBuffer[index++]);
            }
        }

        return bmpResult;
    }



    // deviceColorType,0黑白，1黑白红，2黑白黄，3黑白红黄，5黑白红黄绿蓝橙色
    // useCoefficient 是否使用扩散
    public static Bitmap PD7(Bitmap srcBitmp, int deviceColorType, int ditherType) {

        if (srcBitmp == null)
            return null;

        try {
            int pSrcBitmapBitmap[] = new int[srcBitmp.getWidth() * srcBitmp.getHeight()];
            srcBitmp.getPixels(pSrcBitmapBitmap, 0, srcBitmp.getWidth(), 0, 0, srcBitmp.getWidth(), srcBitmp.getHeight());


            int[] pResultBuffer = BLEControlJni.PicDither7(pSrcBitmapBitmap,
                    srcBitmp.getWidth(), srcBitmp.getHeight(),
                    deviceColorType, ditherType);
            if (pResultBuffer == null || pResultBuffer.length != pSrcBitmapBitmap.length)
                return null;

            int cols = srcBitmp.getWidth();
            int rows = srcBitmp.getHeight();
            Bitmap bmpResult = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
            int index = 0;
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    int rgb = pResultBuffer[index++];
                    int r = Color.red(rgb);
                    int g = Color.green(rgb);
                    int b = Color.blue(rgb);
                    bmpResult.setPixel(col, row, Color.rgb(r, g, b));
                }
            }

            return bmpResult;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static List<Integer> GetSendPicData(Bitmap bitmapBWPic_A, HH_EnumDeviceColorType colorType) {

        List<Integer> list = new ArrayList<>();
        int[] pResultBuffer = new int[1];
        int pBitmapBuffer_A[] = new int[bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight()];
        bitmapBWPic_A.getPixels(pBitmapBuffer_A, 0, bitmapBWPic_A.getWidth(), 0, 0, bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight());

        for (int i = 0; i < bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(); i++){
            pBitmapBuffer_A[i] = ColorConvert(pBitmapBuffer_A[i] );
        }

        pResultBuffer = BLEControlJni.GetPicData(1,pBitmapBuffer_A,
                bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(),
                bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight(), colorType.ordinal(),
                0, 0, 0,
                null,
                bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(),
                bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight(), colorType.ordinal(),
                0, 0, 0);
        for (int i = 0; i < pResultBuffer.length; i++) {
            list.add(pResultBuffer[i]);
        }
        return list;
    }


    public static List<Integer> GetSendPicData(Bitmap bitmapBWPic_A, HH_EnumDeviceColorType colorType, boolean useCompress) {

        List<Integer> list = new ArrayList<>();
        int[] pResultBuffer = new int[1];
        int pBitmapBuffer_A[] = new int[bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight()];
        bitmapBWPic_A.getPixels(pBitmapBuffer_A, 0, bitmapBWPic_A.getWidth(), 0, 0, bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight());

        for (int i = 0; i < bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(); i++){
            pBitmapBuffer_A[i] = ColorConvert(pBitmapBuffer_A[i] );
        }

        pResultBuffer = BLEControlJni.GetPicData2(1,pBitmapBuffer_A,
                bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(),
                bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight(), colorType.ordinal(),
                0, 0, 0,
                null,
                bitmapBWPic_A.getWidth() * bitmapBWPic_A.getHeight(),
                bitmapBWPic_A.getWidth(), bitmapBWPic_A.getHeight(), colorType.ordinal(),
                0, 0, 0,
                useCompress ? 1 : 0);
        for (int i = 0; i < pResultBuffer.length; i++) {
            list.add(pResultBuffer[i]);
        }
        return list;
    }


    // 取中间的一部分
    public static Bitmap cropBitmap2(Bitmap bitmap, int x, int y, int w, int h) {
        if (bitmap == null) {
            return null;
        }
        return Bitmap.createBitmap(bitmap, x, y, w, h, null, false);
    }


    /**
     *      * 头像切成圆形显示
     *
     **/
    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        //圆形图片宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //正方形的边长
        int r = 0;
        //取最短边做边长
        if (width > height) {
            r = height;
        } else {
            r = width;
        }
        //构建一个bitmap
        Bitmap backgroundBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas = new Canvas(backgroundBmp);
        Paint paint = new Paint();
        //设置边缘光滑，去掉锯齿
        paint.setAntiAlias(true);
        //宽高相等，即正方形
        RectF rect = new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //且都等于r/2时，画出来的圆角矩形就是圆形
        canvas.drawRoundRect(rect, r / 2, r / 2, paint);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, paint);

        paint.setColor(Color.parseColor("#0000FF"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        canvas.drawCircle(r / 2, r / 2, r / 2, paint);

        //返回已经绘画好的backgroundBmp
        return backgroundBmp;
    }


    // 等比例缩放图片
    public static Bitmap PC_MadeTextBmp(int width, int height,
                                        String info,
                                        int fontSize,
                                        int r, int g, int b,
                                        boolean bold,
                                        boolean italic,
                                        boolean underline) {

        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);


        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setStyle(Paint.Style.FILL); // 空心样式
        paint.setStrokeWidth(1);//设置线宽w
        paint.setColor(Color.rgb(r, g, b));
        paint.setAntiAlias(false);// 关掉抗锯齿
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTypeface(Typeface.DEFAULT);
        paint.setTextSize(fontSize);
        paint.setFakeBoldText(bold); // 设置粗体
        paint.setUnderlineText(underline); // 下划线
        if (italic)
            paint.setTextSkewX(-0.5f);// 负数表示右斜，整数左斜

        Rect rectDrawTxt = new Rect();
        paint.getTextBounds(info, 0, info.length(), rectDrawTxt);
        int nTxtCenterPointX = width / 2;
        int nTxtCenterPointY = height / 2;
        int fontW = rectDrawTxt.width();
        int fontH = rectDrawTxt.height();
        if (rectDrawTxt.width() > width) {
            // 文字太长，换行
            canvas.save();
            TextPaint textPaint = new TextPaint();
            textPaint.setTextSize(fontSize);
            textPaint.setColor(Color.rgb(r, g, b));
            StaticLayout sl = new StaticLayout(info, textPaint, width, Layout.Alignment.ALIGN_NORMAL,1.0f,0.0f,true);
            canvas.translate(0,0);
            sl.draw(canvas);
            canvas.restore();
        } else {
            Paint.FontMetrics fontMetrics=paint.getFontMetrics();
            float distance=(fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
            float baseline=nTxtCenterPointY+distance;
            canvas.drawText(info, nTxtCenterPointX - fontW / 2, baseline, paint);
        }

        return bmpRet;
    }



    // 等比例缩放图片 四周是圆角
    public static Bitmap zoomImg2A(Bitmap bm, int width, int height, int fillColor_r, int fillColor_g, int fillColor_b) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
        canvas.drawRGB(fillColor_r, fillColor_g, fillColor_b);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);

        return bmpRet;
    }



    // 等比例缩放图片 四周是圆角
    public static Bitmap zoomImg2R(Bitmap bm, int width, int height, int fillColor_r, int fillColor_g, int fillColor_b) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
        canvas.drawRGB(fillColor_r, fillColor_g, fillColor_b);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);

        bmpRet = getRoundedCornerBitmap(bmpRet, 50);

        return bmpRet;
    }


    // 等比例缩放图片
    public static Bitmap zoomImg2(Bitmap bm, int width, int height) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);
        return bmpRet;
    }


    // 等比例缩放图片
    public static Bitmap zoomImg2_png(Bitmap bm, int width, int height) {
        if (bm == null) {
            return null;
        }
        // 得到画的位置
        Rect rz = GetZoomPicSite(bm.getWidth(), bm.getHeight(), width, height);
        Bitmap bmZoom = scaleCanvas(bm, new Rect(0, 0, rz.width(), rz.height()));
        Bitmap bmpRet = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpRet);
        canvas.drawBitmap(bmZoom, rz.left, rz.top, null);
        return bmpRet;
    }


    public static Bitmap GetBitmapFormCustomLayerInfo(List<HH_T_CustomLayerInfo> list, Rect rectClear, int tagW, int tagH) {
        if (list == null || list.size() <= 0)
            return null;

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        Bitmap bitmap2 = Bitmap.createBitmap(rectClear.width(), rectClear.height(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(bitmap2);
        canvas2.drawColor(Color.WHITE);


        for (int i = 0; i < list.size(); i++){
            HH_T_CustomLayerInfo tc = list.get(i);
            switch (tc.getLayerType()){
                case TYPE_BKG:{
                    Bitmap bitmap = Bitmap.createBitmap(list.get(i).getBitmap().getWidth(), list.get(i).getBitmap().getHeight(),
                            Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    canvas.drawColor(Color.WHITE);
                    canvas.drawBitmap(list.get(i).getBitmap(), list.get(i).matrix, paint);

                    // 去掉控件中因为等比例缩放需要切换的图片
                    canvas2.drawBitmap(bitmap, rectClear, new Rect(0,0,rectClear.width(), rectClear.height()), paint);
                    break;
                }
                case TYPE_TXT:
                case TYPE_PIC:{
                    Matrix matrix = new Matrix();

                    matrix.set(tc.matrix);

                    matrix.postTranslate(-rectClear.left, -rectClear.top);
                    canvas2.drawBitmap(tc.getBitmap(), matrix, paint);

                    break;
                }
            }
        }

        // 将控件图片等比例缩放为实际刷图需要的图片
        bitmap2 = zoomImg2(bitmap2, tagW, tagH, 0, 0, 0);

        // 这是最终生成的图片
        //String path = SystemUtil.GetTempPath() + TimeUtil.GetCurrentSystemTimeF() + ".png";
        //HH_PicUtils.BitmapSaveFile(bitmap2, path);

        return bitmap2;
    }

    public static List<HH_T_RGB_TJ> GetPicColorTypeCount(Bitmap b1) {
        if (b1 == null)
            return null;

        List<HH_T_RGB_TJ> list = new ArrayList<>();

        int count = b1.getWidth() * b1.getHeight();
        int pBitmapBuffer_A[] = new int[count];
        b1.getPixels(pBitmapBuffer_A, 0, b1.getWidth(), 0, 0, b1.getWidth(), b1.getHeight());

        for (int i = 0; i < count; i++){
            int r = Color.red(pBitmapBuffer_A[i]);
            int g = Color.green(pBitmapBuffer_A[i]);
            int b = Color.blue(pBitmapBuffer_A[i]);

            boolean bFind = false;
            for (int j = 0; j < list.size(); j++){
                HH_T_RGB_TJ tr = list.get(j);
                if (tr != null){
                    if (tr.getR() == r && tr.getG() == g && tr.getB() == b){
                        int t = list.get(j).getCount();
                        list.get(j).setCount(t + 1);
                        bFind = true;
                        break;
                    }
                }
            }

            if (!bFind){
                HH_T_RGB_TJ tr = new HH_T_RGB_TJ(r, g, b, 0);
                list.add(tr);
            }
        }

        return list;
    }

    /**
     * 将原始 Bitmap 转换为圆角图片（这里实际是圆形图片）
     *
     * @param bitmap 原始图片
     * @param radius 圆角半径
     * @return 圆角（圆形）图片
     */
    private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float radius) {
        // 获取原始图片的宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // 创建一个新的 Bitmap，用于保存圆角效果的图像
        // 使用 ARGB_8888 配置，支持透明度，效果较好
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        // 创建一个 Canvas 对象，用于在新 Bitmap 上绘制
        Canvas canvas = new Canvas(output);

        // 创建 Paint 对象，设置绘制样式
        Paint paint = new Paint();
        paint.setAntiAlias(true); // 启用抗锯齿，以确保绘制出来的圆角平滑
        paint.setFilterBitmap(true); // 启用图像过滤，提高图像质量
        paint.setDither(true); // 启用抖动，使图像颜色更平滑
        paint.setColor(Color.BLACK); // 设置画笔颜色为黑色

        // 创建一个矩形区域，作为圆角矩形的边界
        RectF rectF = new RectF(0, 0, width, height);

        // 绘制一个圆角矩形（实际绘制的是一个圆形矩形）
        // 这里的 radius 就是圆角的半径，设置为较大的值会形成圆形效果
        canvas.drawRoundRect(rectF, radius, radius, paint);

        // 设置 Xfermode（混合模式），使得图片绘制在圆角矩形内
        // SRC_IN 表示仅保留矩形与原图片交集部分的内容，超出部分透明
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        // 将原始图片绘制到圆角矩形区域内
        canvas.drawBitmap(bitmap, 0, 0, paint);

        // 返回处理后的圆角图片
        return output;
    }


    // 标准转i1,或i1转标准
    public static int ColorConvert(int color){
        if (color == rgbBlack.getColor())
            return rgbBlack_I1.getColor();
        else  if (color == rgbBlack_I1.getColor())
            return rgbBlack.getColor();

        else if (color == rgbWhite.getColor())
            return rgbWhite_I1.getColor();
        else  if (color == rgbWhite_I1.getColor())
            return rgbWhite.getColor();

        else if (color == rgbRed.getColor())
            return rgbRed_I1.getColor();
        else  if (color == rgbRed_I1.getColor())
            return rgbRed.getColor();


        else if (color == rgbGreen.getColor())
            return rgbGreen_I1.getColor();
        else  if (color == rgbGreen_I1.getColor())
            return rgbGreen.getColor();


        else if (color == rgbBlue.getColor())
            return rgbBlue_I1.getColor();
        else  if (color == rgbBlue_I1.getColor())
            return rgbBlue.getColor();


        else if (color == rgbYellow.getColor())
            return rgbYellow_I1.getColor();
        else  if (color == rgbYellow_I1.getColor())
            return rgbYellow.getColor();


        else if (color == rgbOrange.getColor())
            return rgbOrange_I1.getColor();
        else  if (color == rgbOrange_I1.getColor())
            return rgbOrange.getColor();

        return rgbBlack.getColor();
    }


    // 标准转i1,或i1转标准
    public static HH_T_RGB ColorConvert(HH_T_RGB color){
        if (color.getColor() == rgbBlack.getColor())
            return rgbBlack_I1;
        else  if (color.getColor() == rgbBlack_I1.getColor())
            return rgbBlack;

        else if (color.getColor() == rgbWhite.getColor())
            return rgbWhite_I1;
        else  if (color.getColor() == rgbWhite_I1.getColor())
            return rgbWhite;

        else if (color.getColor() == rgbRed.getColor())
            return rgbRed_I1;
        else  if (color.getColor() == rgbRed_I1.getColor())
            return rgbRed;


        else if (color.getColor() == rgbGreen.getColor())
            return rgbGreen_I1;
        else  if (color.getColor() == rgbGreen_I1.getColor())
            return rgbGreen;


        else if (color.getColor() == rgbBlue.getColor())
            return rgbBlue_I1;
        else  if (color.getColor() == rgbBlue_I1.getColor())
            return rgbBlue;


        else if (color.getColor() == rgbYellow.getColor())
            return rgbYellow_I1;
        else  if (color.getColor() == rgbYellow_I1.getColor())
            return rgbYellow;


        else if (color.getColor() == rgbOrange.getColor())
            return rgbOrange_I1;
        else  if (color.getColor() == rgbOrange_I1.getColor())
            return rgbOrange;

        return rgbBlack;
    }

    public static Bitmap ChangeBitmapColor(Bitmap srcBitmp, HH_EnumDeviceColorType deviceColorType) {
        if (srcBitmp == null)
            return null;

        for (int i = 0; i < srcBitmp.getWidth(); i++) {
            for (int j = 0; j < srcBitmp.getHeight(); j++) {
                //int r = Color.red(srcBitmp.getPixel(i, j));
                //int g = Color.green(srcBitmp.getPixel(i, j));
                //int b = Color.blue(srcBitmp.getPixel(i, j));
                //HH_T_RGB rgb = new HH_T_RGB(r, g, b);
                //HH_Logging.i(TAG, "%03d,%03d,%03d", r, g, b);

                switch (deviceColorType){
                    case BLACK_WHITE_0:{
                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else {
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        }
                        break;
                    }
                    case BLACK_WHITE_YELLOW_1:{
                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbWhite.getColor()){
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        } else {
                            srcBitmp.setPixel(i, j, rgbYellow_I1.getColor());
                        }
                        break;
                    }
                    case BLACK_WHITE_RED_2:{
                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbWhite.getColor()){
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        } else {
                            srcBitmp.setPixel(i, j, rgbRed_I1.getColor());
                        }
                        break;
                    }
                    case BLACK_WHITE_RED_YELLOW_3:{

                        /*HH_Logging.i(TAG,  "%d=(%d,%d,%d)",
                                srcBitmp.getPixel(i, j),
                                rgbBlack.getColor(),
                                rgbWhite.getColor(),
                                rgbRed.getColor());

                        int x1 = srcBitmp.getPixel(i, j);
                        int x2 = rgbBlack.getColor();*/

                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbWhite.getColor()){
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbRed.getColor()){
                            srcBitmp.setPixel(i, j, rgbRed_I1.getColor());
                        }else {
                            srcBitmp.setPixel(i, j, rgbYellow_I1.getColor());
                        }
                        break;
                    }
                    case N_COLOR_4:{
                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbWhite.getColor()){
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbRed.getColor()){
                            srcBitmp.setPixel(i, j, rgbRed_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbYellow.getColor()){
                            srcBitmp.setPixel(i, j, rgbYellow_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbGreen.getColor()){
                            srcBitmp.setPixel(i, j, rgbGreen_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbBlue.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlue_I1.getColor());
                        } else {
                            srcBitmp.setPixel(i, j, rgbOrange_I1.getColor());
                        }
                        break;
                    }
                    case E6_5:{
                        if (srcBitmp.getPixel(i, j) == rgbBlack.getColor()){
                            srcBitmp.setPixel(i, j, rgbBlack_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbWhite.getColor()){
                            srcBitmp.setPixel(i, j, rgbWhite_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbRed.getColor()){
                            srcBitmp.setPixel(i, j, rgbRed_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbYellow.getColor()){
                            srcBitmp.setPixel(i, j, rgbYellow_I1.getColor());
                        } else if (srcBitmp.getPixel(i, j) == rgbGreen.getColor()){
                            srcBitmp.setPixel(i, j, rgbGreen_I1.getColor());
                        } else {
                            srcBitmp.setPixel(i, j, rgbBlue_I1.getColor());
                        }
                        break;
                    }
                }
            }
        }

        return srcBitmp;
    }



    /**
     * 将Bitmap存为 .bmp格式图片
     *
     * @param bitmap
     */
    public static void saveBmp(Bitmap bitmap, String filename) {
        if (bitmap == null)
            return;
        // 位图大小
        int nBmpWidth = bitmap.getWidth();
        int nBmpHeight = bitmap.getHeight();
        // 图像数据大小
        int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);
        try {
            // 存储文件名
            File file = new File(filename);
            if (!file.exists()) {
                file.createNewFile();
            } else {
                file.delete();
                file.createNewFile(); //重新创建一下，防止没有覆盖
            }
            FileOutputStream fileos = new FileOutputStream(filename);
            // bmp文件头
            int bfType = 0x4d42;
            long bfSize = 14 + 40 + bufferSize;
            int bfReserved1 = 0;
            int bfReserved2 = 0;
            long bfOffBits = 14 + 40;
            // 保存bmp文件头
            writeWord(fileos, bfType);
            writeDword(fileos, bfSize);
            writeWord(fileos, bfReserved1);
            writeWord(fileos, bfReserved2);
            writeDword(fileos, bfOffBits);
            // bmp信息头
            long biSize = 40L;
            long biWidth = nBmpWidth;
            long biHeight = nBmpHeight;
            int biPlanes = 1;
            int biBitCount = 24;
            long biCompression = 0L;
            long biSizeImage = 0L;
            long biXpelsPerMeter = 0L;
            long biYPelsPerMeter = 0L;
            long biClrUsed = 0L;
            long biClrImportant = 0L;
            // 保存bmp信息头
            writeDword(fileos, biSize);
            writeLong(fileos, biWidth);
            writeLong(fileos, biHeight);
            writeWord(fileos, biPlanes);
            writeWord(fileos, biBitCount);
            writeDword(fileos, biCompression);
            writeDword(fileos, biSizeImage);
            writeLong(fileos, biXpelsPerMeter);
            writeLong(fileos, biYPelsPerMeter);
            writeDword(fileos, biClrUsed);
            writeDword(fileos, biClrImportant);

            byte bmpData[] = new byte[bufferSize];
            int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);
            for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol) {
                for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 3) {


                    int clr = bitmap.getPixel(wRow, nCol);
                    bmpData[nRealCol * wWidth + wByteIdex] = (byte) Color.blue(clr);
                    bmpData[nRealCol * wWidth + wByteIdex + 1] = (byte) Color.green(clr);
                    bmpData[nRealCol * wWidth + wByteIdex + 2] = (byte) Color.red(clr);
                }
            }
            fileos.write(bmpData);
            fileos.flush();
            fileos.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void writeWord(FileOutputStream stream, int value) throws IOException {
        byte[] b = new byte[2];
        b[0] = (byte) (value & 0xff);
        b[1] = (byte) (value >> 8 & 0xff);
        stream.write(b);
    }

    public static void writeDword(FileOutputStream stream, long value) throws IOException {
        byte[] b = new byte[4];
        b[0] = (byte) (value & 0xff);
        b[1] = (byte) (value >> 8 & 0xff);
        b[2] = (byte) (value >> 16 & 0xff);
        b[3] = (byte) (value >> 24 & 0xff);
        stream.write(b);
    }

    public static void writeLong(FileOutputStream stream, long value) throws IOException {
        byte[] b = new byte[4];
        b[0] = (byte) (value & 0xff);
        b[1] = (byte) (value >> 8 & 0xff);
        b[2] = (byte) (value >> 16 & 0xff);
        b[3] = (byte) (value >> 24 & 0xff);
        stream.write(b);
    }

}